package com.spzx.payment.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.rabbit.constant.MqConst;
import com.spzx.common.rabbit.service.RabbitService;
import com.spzx.order.api.RemoteOrderInfoService;
import com.spzx.order.domain.OrderInfo;
import com.spzx.order.domain.OrderItem;
import com.spzx.payment.domain.PaymentInfo;
import com.spzx.payment.mapper.PaymentInfoMapper;
import com.spzx.payment.service.IPaymentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.SecurityContext;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * 付款信息Service业务层处理
 *
 * @author atguigu
 * @date 2024-07-22
 */
@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements IPaymentInfoService
{

    @Autowired
    private RemoteOrderInfoService remoteOrderInfoService;

    @Autowired
    private RabbitService rabbitService;

    @Autowired
    private PaymentInfoMapper paymentInfoMapper;


    @Override
    public PaymentInfo savePaymentinfo(String orderNo) {

        R<OrderInfo> infoR = remoteOrderInfoService.getByOrderNo(orderNo, SecurityConstants.INNER);
        OrderInfo orderInfo = infoR.getData();
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(new LambdaQueryWrapper<PaymentInfo>()
                .eq(PaymentInfo::getOrderNo, orderNo));
        if (paymentInfo == null){
            paymentInfo = new PaymentInfo();

            paymentInfo.setUserId(orderInfo.getUserId());


            //TODO   String content = "";
            //        for(OrderItem item : orderInfo.getOrderItemList()) {
            //            content += item.getSkuName() + " ";
            //        }


            StringBuilder content = new StringBuilder();
            for (OrderItem orderItem : orderInfo.getOrderItemList()) {
                content.append(orderItem.getSkuName());
            }

            paymentInfo.setContent(content.toString());
            paymentInfo.setAmount(orderInfo.getTotalAmount());
            paymentInfo.setPaymentStatus("0");
            paymentInfo.setOrderNo(orderNo);

            paymentInfoMapper.insert(paymentInfo);

        }


        return paymentInfo;
    }

    @Override
    public void updatePaymentStatus(Map<String, String> paramMap) {
        String tradeNo = paramMap.get("out_trade_no");

        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(new LambdaQueryWrapper<PaymentInfo>().eq(PaymentInfo::getTradeNo, tradeNo));
        if ("1".equals(paymentInfo.getTradeNo())){
            return;
        }
        //更新已经支付状态
        paymentInfo.setPaymentStatus("1");
        paymentInfo.setPayType(2L);//支付宝方式  2
        paymentInfo.setTradeNo(tradeNo);
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(JSON.toJSONString(paramMap));
        paymentInfoMapper.updateById(paymentInfo);

        rabbitService.sendMessage(MqConst.EXCHANGE_PAYMENT_PAY,MqConst.ROUTING_PAYMENT_PAY,paymentInfo.getOrderNo());



    }
}
